// pages/search/search.js
import { getArticleList, getArticlePage } from '../../api/article/article'
import { saveCollect, delCollect } from '../../api/article/article'
import { _t } from '../../i18n/base'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
  data: {
    articleReqEvent: {
      categoryId: '',
      categoryType: '',
      cityId: '',
      industryId: '',
      title: '',
      userId: '',
      page: 1,
      size: 20,
    },
    pageLoading: false,
    totalCount: 0,
    value: '',
    momentList: [],
    messagePath: '/pages/overview/overview',
    messageImg: '/images/logo新.png',
  },
  // 获取文章列表
  async getArticlePage(e) {
    if (!this.data.pageLoading) {
      this.setData({
        ['articleReqEvent.page']: 1,
      })
    }
    let { result } = await getArticlePage(e)
    let data = {
      pageLoading: false,
      totalCount: result.totalCount,
    }
    if (this.data.pageLoading) {
      data.momentList = this.data.momentList.concat(result.content)
    } else {
      data.momentList = result.content
    }
    this.setData(data)
  },
  // 滚动加载
  scrollEnd() {
    if (!this.data.pageLoading && this.data.momentList.length < this.data.totalCount) {
      this.setData({
        ['articleReqEvent.page']: this.data.articleReqEvent.page + 1,
        pageLoading: true,
      })
      this.getArticlePage(this.data.articleReqEvent)
    }
  },
  // 跳转客服
  toMomentDetailHandle({ detail }) {
    Dialog.confirm({
      title: this.data.lang['查看内容'],
      message: this.data.lang['跳转内容链接'],
      confirmButtonText: this.data.lang['确定'],
      cancelButtonText: this.data.lang['取消'],
      confirmButtonOpenType: 'contact',
      sendMessageTitle: `${detail.title}`,
      sendMessagePath: `${this.data.messagePath}?${detail.momentId},${detail.type}`,
      sendMessageImg: this.data.messageImg,
      showMessageCard: true,
    })
      .then(async () => {
        setTimeout(() => {
          console.log(12)
        })
      })
      .catch(() => {})
  },
  // 收藏
  async toCollectHandle({ detail }) {
    this.setData({
      ['articleReqEvent.articleId']: detail.momentId,
    })
    if (detail.collect == 1) {
      //取消收藏
      let { result } = await delCollect({ ids: detail.collectId })
      if (result) {
        this.getArticlePage(this.data.articleReqEvent)
      }
    } else {
      if (wx.getStorageSync('userId')) {
        let { result } = await saveCollect(this.data.articleReqEvent)
        if (result) {
          this.setData({
            collectStatus: result.status,
          })
          this.getArticlePage(this.data.articleReqEvent)
        }
      } else {
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }
    }
  },
  // 转发复制链接
  toForwardHandle({ detail }) {
    wx.setClipboardData({
      data: detail,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 搜索
  toSearch({ detail }) {
    this.setData({
      ['articleReqEvent.title']: detail,
    })
    this.getArticlePage(this.data.articleReqEvent)
  },
  // 取消搜索
  onCancel() {
    this.setData({
      momentList: [],
    })
  },
  onLoad() {
    let language = wx.getStorageSync('lang')
    let userId = wx.getStorageSync('userId')
    let lang = _t()
    this.setData({
      lang,
      // ['articleReqEvent.language']: language,
      ['articleReqEvent.userId']: userId,
    })
    wx.setNavigationBarTitle({
      title: lang['搜索'],
    })
  },
})
